#!/usr/bin/env python

#This script synchronizes the home directory with the backup directory
# and puts backups of changed and deleted files into a folder with the
# jkag-increments directory

import os
from os import path
from time import asctime
import restformat
import sys, socket


# Name of the machine being backed up
hostname = socket.gethostname()

# Type of the OS being backed up
os_type = sys.platform
if os_type.find('darwin') >= 0:
    # This is a Mac OS X
    root_dir = '/'


# Get the user input of where the user is
#location = int(raw_input("Where is the location now?\n1:Home\n2:ICR WITH external HDD access\n3:ICR WITHOUT external HDD access\nEnter: "))


#Variables
backupruntime_asc = asctime()
backupruntime_wwh = backupruntime_asc.replace(' ', '-')
backupruntime = backupruntime_wwh.replace(':', '.')


# Source Directories
homedir = os.path.expanduser('~')
sourcedir = path.join(homedir, 'workspace')
backupruntime_gz = backupruntime + '.gz'
backup_workspace = path.join(homedir, 'backup_workspace')
backupdir = path.join(homedir, 'backup_workspace', hostname)
backupdir_gz = path.join(backupdir, backupruntime_gz)


# Archive the source directory
if path.exists(backup_workspace):
    print 'backup_workspace already exists\n continuing...'
else:
    os.mkdir(backup_workspace)

if path.exists(backupdir):
    print 'backup_workspace/"hostname"/ already exists\n continuing...'
else:
    os.mkdir(backupdir)


# Logging START
logfile = path.join(backupdir, 'hoo-backup.py.log')
if path.exists(logfile):
    backuplog = open(logfile, 'a')
else:
    backuplog = open(logfile, 'w')
    backuplog.write('Backup Log created on ' + backupruntime_asc + '\n')
    backuplog.write('by the user: ' + os.getlogin() + '\n')
    backuplog.write('by the program: ' + 'hoo-backup.py - ver.0.1 - Apr.19.2010\n')
    backuplog.write('Machine name: ' + hostname + '\n\n')
    backuplog.close()
    backuplog = open(logfile, 'a')
print >> backuplog, (restformat.title(backupruntime))
print >> backuplog, (restformat.subtitle('BEGIN BACKUP'))


# Make the instruction set, write on logfile, excute, and write on log file
tar_instruction = 'tar --exclude=".*" -zcvf ' + backupdir_gz + ' ' + sourcedir + ' >> ' + logfile
print >> backuplog, (tar_instruction)
backuplog.close()

os.system(tar_instruction)


# Run copy and rsync backup command to backup to the CLINMAG
backup_dir_in_CLINMAG = path.join(root_dir, 'Volumes', 'CLINMAG', 'hshin', 'hoo-backups') # It's Shared:\Clinmag on Windows
machine_dir_folder_in_CLINMAG = path.join(backup_dir_in_CLINMAG, hostname)
machine_dir_folder_in_CLINMAG_gz = path.join(machine_dir_folder_in_CLINMAG, backupruntime_gz)

if path.exists(backup_dir_in_CLINMAG):
    print 'hoo-backups already exists in CLINMAG\n continuing...'
else:
    os.mkdir(path.join(backup_dir_in_CLINMAG))
    print ('First time backup on CLINMAG server.\n Folder created\n continuing...')
    
if path.exists(machine_dir_folder_in_CLINMAG):
    print 'hoo-backups/"hostname" already exists in CLINMAG\n continuing...'
else:
    os.mkdir(machine_dir_folder_in_CLINMAG)
    print ('First time backup of this machine on CLINMAG server.\n Folder created\n continuing...')

print backupdir_gz
print machine_dir_folder_in_CLINMAG_gz

cp_clinmag_instruction = \
    'cp ' + backupdir_gz + ' ' + machine_dir_folder_in_CLINMAG + ' >> ' + logfile
    
cp_clinmag_log_instruction = \
    'cp ' + logfile + ' ' + machine_dir_folder_in_CLINMAG

rsync_clinmag_instruction = \
    'rsync -avb --delete --exclude ".*" ' + backupdir_gz + ' ' + machine_dir_folder_in_CLINMAG + ' >> ' + logfile
    #'rsync -avb --delete --backup-dir=' + '"' + machine_dir_folder_in_CLINMAG_gz + '" ' + backupdir_gz + ' >> ' + logfile
    
rsync_clinmag_log_instruction = \
    'rsync -avb --delete ' + backupdir_gz + ' ' + machine_dir_folder_in_CLINMAG + ' ' + logfile
    #'rsync -avb --delete --backup-dir=' + '"' + machine_dir_folder_in_CLINMAG + '" ' + logfile

os.system(cp_clinmag_instruction)
backuplog = open(logfile, 'a')
print >> backuplog, (cp_clinmag_instruction)


# Logging END
backuplog = open(logfile, 'a')
print >> backuplog, (restformat.subtitle('END BACKUP'))
print >> backuplog, ('\n\n')
backuplog.close()

os.system(cp_clinmag_log_instruction)


## Backup Directories
#backupdir = "/Volumes/CLINMAG/hshin/rsync-backups/"
#incrementsdir = '/Volumes/CLINMAG/hshin/rsync-backups/hshin-increments/' + backupruntime + '/'
#
#
##Logging
#backuplog = open('/Volumes/CLINMAG/hshin/rsync-backups/rsync-backup.py.log', 'a')
#print >> backuplog, (restformat.title(backupruntime))
#print >> backuplog, ('Making increments directory:"' + incrementsdir + '"\n')
#
##Make incrementsdir
#os.mkdir(incrementsdir)
#
##Logging
#print >> backuplog, (restformat.subtitle('BEGIN BACKUP'))
#backuplog.close()
#
##Run rsync backup command with Unix logging
#os.system('rsync -avb --delete --exclude ".*" --backup-dir=' + '"' + incrementsdir + '" ' + sourcedir +
#          ' ' + backupdir + '>> ' + '/Volumes/CLINMAG/hshin/rsync-backups/rsync-backup.py.log')
#
##Logging
#backuplog = open('/Volumes/CLINMAG/hshin/rsync-backups/rsync-backup.py.log', 'a')
#print >> backuplog, (restformat.subtitle ('END BACKUP'))
#print >> backuplog, ('\n\n')
#backuplog.close()

